package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Article;
import ysq.xyg.model.BaseMenu;
import ysq.xyg.model.info.BaseUserInfo;
import ysq.xyg.service.ArticleService;
import ysq.xyg.service.BaseMenuService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-03-22 11:57
 **/
@Controller
public class CommonController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(CommonController.class);
    @Resource
    private BaseMenuService baseMenuService;
    @Resource
    private ArticleService articleService;
    /**
     * 登录页面（默认是首页）
     * */
    @RequestMapping(value="/")
    public ModelAndView login(HttpServletRequest request,HttpServletResponse response){
        BaseUserInfo baseUserInfo=new BaseUserInfo();
        baseUserInfo.setUserid(BaseController.CookiesAcceptObject(request, BaseConstant.COOKIE_LOGINNAME));
        baseUserInfo.setUserpassword(BaseController.CookiesAcceptObject(request, BaseConstant.COOKIE_PASSWORD));
        if(baseUserInfo.getUserid()!=null && !"".equals(baseUserInfo.getUserid()) && baseUserInfo.getUserpassword()!=null && !"".equals(baseUserInfo.getUserpassword())){
            baseUserInfo.setAutoLogin(2);
        }else{
            baseUserInfo.setAutoLogin(1);
        }
        log.info("baseUserInfo-"+baseUserInfo.toString());
        return new ModelAndView("/login").addObject(baseUserInfo);
    }
    /**
     * 登录进入默认页面
     * */
    @RequestMapping(value="/main")
    public ModelAndView main(HttpServletRequest request,HttpServletResponse response){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        Date newDate=new Date();
        List<String> dataList=new ArrayList<String>();
        List<Integer> tsList=new ArrayList<Integer>();
        List<Integer> sbList=new ArrayList<Integer>();
        Article article=new Article();
        //类型：1、投诉，2、上报
        article.setType("1");
        Integer currentAllTs=articleService.selectEntityCount(article);
        article.setType("2");
        Integer currentAllSb=articleService.selectEntityCount(article);

        //获取最近7天
        for(int i=0;i<7;i++){
            dataList.add(DateUtil.DateToStr(DateUtil.getDateOfDate(newDate,i), DateUtil.DATE_PATTERN));
            Article articleTs=new Article();
            articleTs.setType("1");
            articleTs.setStartTime(dataList.get(i)+" 00:00:00");
            articleTs.setEndTime(dataList.get(i)+" 23:59:59");
            int  tsSize= articleService.selectEntityCount(articleTs);

            articleTs.setType("2");
            int  sbSize= articleService.selectEntityCount(articleTs);
            tsList.add(tsSize);
            sbList.add(sbSize);
        }

        return new ModelAndView("/baseMain").addObject("baseUserInfo",baseUserInfo)
                .addObject("currentAllTs",currentAllTs)
                .addObject("currentAllSb",currentAllSb)
                .addObject("dataList", JSONArray.toJSONString(dataList))
                .addObject("tsList", JSONArray.toJSONString(tsList))
                .addObject("sbList",JSONArray.toJSONString(sbList))
                ;
    }

    /**
     * 个人中心页面
     * */
    @RequestMapping(value="/index")
    public ModelAndView Index(HttpServletRequest request,HttpServletResponse response){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        //菜单查询
        BaseMenu baseMenu=new BaseMenu();
        baseMenu.setRemovetag(BaseConstant.REMOVE_N);
        baseMenu.setMenugrade(BaseConstant.MENU_ONE);
        baseMenu.setDepartId(baseUserInfo.getDepartid());
        List<BaseMenu> baseMenuList=baseMenuService.selectEntityByLeft(baseMenu);
        return new ModelAndView("/index").addObject("baseMenuList",baseMenuList).addObject("baseMenu",baseMenuList.get(0)).addObject("baseUserInfo",baseUserInfo);
    }

}
